import axios from "../utils/axios.min.js";
import {wxLogin} from "./login.js"
// import { getJwtToken } from "./jwt";
const getUserToken = () => {
	return uni.getStorageSync("token");
};
const clearUserToken = () => {
	uni.removeStorage({
		key: 'token',
		success: function (res) {
			console.log('发现账号异地登录或者token过期')
		},
		fail(err) {
			console.log(err)
		}
	});
}

const loginAgain = () =>{
	uni.login({
		provider:"weixin",
		success:(login_ret => {
			let params = {code:login_ret.code,}
			wxLogin(params).then((userlogin) => {
				if(userlogin.data.code == 0){
					uni.setStorageSync('token',userlogin.data.data.token)
					uni.setStorageSync('phone',userlogin.data.data.phone)
					uni.setStorageSync('avatar',userlogin.data.data.avatar)
					uni.setStorageSync('nickname',userlogin.data.data.nickname)
					return true
				}
				if(userlogin.statusCode == 403){
					// response.data.login = true
					return false
				}
			}).catch((error) => {
				console.log(error)
			})
		})
	})
}

let baseURL = "https://hxt.daoyintc.com/hxt" //远程


if (process && process.env && process.env.NODE_ENV === "development") {
	axios.defaults.baseURL = baseURL; 
} else {
	axios.defaults.baseURL = baseURL;
}

axios.defaults.timeout = 3000;

axios.defaults.adapter = config => {
	return new Promise((resolve, reject) => {
		let httpConfig = {
			url: config.url,
			method: config.method,
			data: config.data,
			header: config.headers,
			success: res => {
				resolve(res);
			},
			fail: err => {
				reject(err);
			}
		};
		uni.request(httpConfig)
	});
};

// axios.defaults.headers.post['content-type'] = 'application/x-www-form-urlencoded';
// 对request做处理(比如加密等)
// // 目前只在Header中填入JwtToken(注意uni.request的封装)
axios.interceptors.request.use(
	(request) => {
		var token = getUserToken();
		if (token) {
			request.headers.Authorization = `${token}`;
		} else if (!token) {
			request.headers.Authorization = null;
		}
		return request;
	},
	err => Promise.reject(err)
);

// 对response做处理,主要针对uni.request(success的返回做处理)
// 只有当状态码为200时进行处理
axios.interceptors.response.use(
	response => {
		if (response.statusCode === 200) {
			return response.data;
		} else {
			// 重新登录
			if (response.statusCode === 401) {
				console.log("重新登录------------")
				// var loginStatus = loginAgain()
			}
			return Promise.reject(response);
		}
	},
	err => {
		console.log(err)
		return Promise.reject(err)
	}
);

export const get = (url, params) => {
	return axios({
		method: "GET",
		url: url,
		data: params
	});
};


export const post = (url, params) => {
	return axios({
		method: "POST",
		url: url,
		data: params
	});
};
export const postFD = (url, params) => {
	console.log(params)
	return axios({
		method: "POST",
		url: url,
		headers:{
			'content-type': 'application/json' 
		},
		data: params,
	});
};

export const del = (url, params) => {
	return axios({
		method: "DELETE",
		url: url,
		data: params
	});
};

export const patch = (url, params) => {
	return axios({
		method: "PATCH",
		url: url,
		data: params
	});
};

export const put = (url, params) => {
	return axios({
		method: "PUT",
		url: url,
		data: params
	});
};
